import { codequery } from '@/api/zt/index'

export default {
  data() {
    return {
      isDisable:false,
      disFlag: false,
      btnStr: '确定'
    }
  },
  created() {
  },
  methods: {
    jsonchange(v){
      if(v && typeof(v)=='object' && !Array.isArray(v)){
        this.isDisable = false
      }else{
        this.isDisable = true
      }
      if(v instanceof Error){
        this.isDisable = true
      }
    },
    inspectformdat(v){
      let flag = true;
      let keys = []
      if(v.length>0){
        keys = Object.keys(v[0])
        for(let i =0;i<v.length;i++){
          if(!v[i]['fieldName']||!v[i]['fieldCnName']||!v[i]['fieldTypeNum']){
            flag = false
            this.$message.error(`请把第${i+1}条数据填写完整！`)
            break
          }
        }
      }
      return flag;
    },
    getValue(v){
      this.innerVisible = false
      if(v=='false'){
        return
      }
      this.ruleForm['cron'] = v
    },
    getcodequery(v) {
      codequery({
        'pageIndex': 1,
        'type': v,
        'pageSize': 1000
      }).then(res => {
        this.selectDataList = res.data.records
      })
    },
    addUser(v) {
      this.selectedRow=null //api应用管理
      this.disFlag = false
      delete this.ruleForm.id
      this.title = '新增'
      this.dialogTableVisible = true


      this.resetForm()
    },
    resetForm(str) {
      const keys = Object.keys(this.ruleForm)
      const o = {}
      keys.forEach((item, index) => {
        const v = this.ruleForm[item]
        if (typeof (v) === 'string') {
            o[item] = ''
        } else if (typeof (v) === 'number') {
          o[item] = ''
        } else if (typeof (v) === 'object') {
          if (v instanceof Array) {
            o[item] = []
          } else{
            o[item] = {}
          }
        }
      })

      this.fields = [
        {
          "fieldCnName": "",
          "fieldName": "",
          "fieldTypeNum": '',
          "fieldType": ""
        }
      ]
      this.txt = ""
      this.resOutInfo = null
      this.gzListOptions = [
        {
          "ruleField":"",
          "quality":""
        }
      ]
      this.ofgzListOptions =[ //实时服务管理
        {
          "fieldName":"",
          "desId":""
        }
      ]
      this.sqlValue = ""
      this.dataList = []
      this.addParms2 = [
        {
          "modelId":'',
          "modelName": "",
          "desensitizationId":'',
          "desensitizationName":""
        }
      ],
      this.ruleForm = JSON.parse(JSON.stringify(o))
      if (str == 'return') {
        this.dialogTableVisible = false
      }
    },
    showLook(v) {
      this.title = ''
      const keys = Object.keys(JSON.parse(v))
      keys.forEach((item, index) => {
        this.$set(this.ruleForm, item, JSON.parse(v)[item])

        if (item == 'examples') {
          this.txt = JSON.parse(v)[item]
        }
        if (item == 'fields') {
          this.fields = JSON.parse(v)[item]
        }
      })
      this.dialogTableVisible = true
    },
    getDataDetileInfo(v) {
      this.disFlag = true
      this.showLook(v)
    },
    editRowData(v) {
      this.selectedRow=null //api应用管理
      this.disFlag = false
      this.title = '修改'
      const keys = Object.keys(JSON.parse(v))
      this.dialogTableVisible = true
      keys.forEach((item, index) => {
        this.$set(this.ruleForm, item, JSON.parse(v)[item])
        if (item == 'examples') {
          this.txt = JSON.parse(v)[item]
        }
        if (item == 'fields') {
          this.fields = JSON.parse(v)[item]
        }
        if (item == 'place') {
          let str = JSON.parse(v)[item]
          this.$set(this.ruleForm, item, str.split(',') )
        }

        if(this.$route.fullPath=='/ykp/decryption/decryptionpages'){
         if(item == 'desensitizationTypeNum'){
             this.ruleForm['desensitizationTypeNum'] = JSON.parse(v)[item]
             this.getChildrenList()
          }else if(item == 'dataModelName'){
            this.listOptions1.forEach((item,index)=>{
              if(item.id==this.ruleForm['dataModelId']){
                this.listOptions2 = item.fields||[]
              }
          })
          }
        }else if(this.$route.fullPath=='/ykp/realtimedata/ztrwgl'){
          if (item == 'qualityRule') {
            let arr = JSON.parse(v)[item]
            if(arr&&arr.length!=0){
              this.gzListOptions = JSON.parse(arr)
            }else{
              this.gzListOptions = [
                {
                  "ruleField":"",
                  "quality":""
                }
              ]
            }
          }else if(item == 'dataTable'){
            let str = JSON.parse(v)[item]
            const regex = /^tb_rt_/;
            let val = ''
            if (!regex.test(str)) {
              val = str
            }else{
              val=str.split('tb_rt_')[1]
            }

            this.$set(this.ruleForm, item, val)
          }else if(item == 'clusterId'){
            this.queryTremOptions(JSON.parse(v)[item])
          }else if(item == 'modelId'){
            this.options3.forEach((item,index)=>{
              if(item.id==this.ruleForm.modelId){
                this.ruleForm.modelName = item.dataModelName
                this.options4 = item.fields
              }else{
                this.options4 = []
              }
            })
          }
        }else if(this.$route.fullPath=='/ykp/dataServer/apiadmin'){
          if (item == 'params') {
            let arr = JSON.parse(v)[item]
            if(arr.length!=0){
              this.addParms2 = JSON.parse(arr)
            }else{
              this.addParms2 = [
                {
                  "modelId":'',
                  "modelName": "",
                  "desensitizationId":'',
                  "desensitizationName":""
                }
              ]
            }
          }else if(item == 'datasourceId'){
            this.getZtreeList(JSON.parse(v)[item])
          }else if(item == 'sqlData'){ //api管理
            this.sqlValue = JSON.parse(v)[item]
          }else if(item == 'appKey'){ //api管理
            this.ruleForm['appKey']  = JSON.parse(JSON.parse(v)[item])
          }
        }else if(this.$route.fullPath=='/ykp/dataServer/oftendataServe'){
          if(item == 'modelId'){
            this.selectDataList.forEach((item,index)=>{
              if(item.id==this.ruleForm.modelId){
                this.zdOptions = item.fields
              }
            })
          }else if(item=='desType'){
            let flag = JSON.parse(v)[item]=='字段脱敏'?false:true
            this.queryTmGz(flag,JSON.parse(JSON.parse(v)['desData']))
          }
        }else if(this.$route.fullPath=='/ykp/norealtimedata/httpaccess'){
          if(item == 'bodyData'){
            this.ruleForm['bodyData'] = JSON.parse(JSON.parse(v)[item])
          }else if (item == 'qualityRule') {
            let arr = JSON.parse(v)[item]
            if(arr&&arr.length!=0){
              this.gzListOptions = JSON.parse(arr)
            }else{
              this.gzListOptions = [
                {
                  "ruleField":"",
                  "quality":""
                }
              ]
            }
          }
        }
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.commitFromData()
        } else {
          return false
        }
      })
    },
    errorDialog(v) {
      this.$notify.error({
        title: '错误',
        message: v
      })
    },
    warningDialog(v) {
      this.$notify({
        title: '警告',
        message: v,
        type: 'warning'
      })
    },
    successDialog(v) {
      this.$notify({
        title: '成功',
        message: v,
        type: 'success'
      })
    }
  }
}
